# OS Environment
ARCH := $(shell uname -m)

GNUSTEP_HOME = /opt/GNUstep/Local/Library

# Project
C_DIR = ../../c
HEADERSDIR = ../include
LIBDIR = ../build/lib
LIBNAME = promoteobjc-${ARCH}
TEST_HEADERS = \
    src/memory/PMTStandardAllocatorTest.h \
    src/testing/PMTTesting.h
TEST_HEADERSDIR = include
TEST_LIBDIR = build/lib
TEST_LIBNAME = testpromoteobjc-${ARCH}
TEST_LIBRARY = ${TEST_LIBDIR}/lib${TEST_LIBNAME}.a
TEST_OBJDIR = build/obj
TEST_OBJS = \
    memory/PMTStandardAllocatorTest.o
TEST_OBJDIROBJS = ${addprefix ${TEST_OBJDIR}/, ${TEST_OBJS}}

# Compiler
CXX = ccache gcc
DEBUG_FLAGS = -g
DEF_FLAGS = -D${ARCH}
INCLUDE_PATH = -I${TEST_HEADERSDIR} -I${HEADERSDIR} -Isrc/testing -I${GNUSTEP_HOME}/Headers -I${C_DIR}/include
LIBRARY_PATH = -L${TEST_LIBDIR} -L${LIBDIR} -L${C_DIR}/build/lib -L${GNUSTEP_HOME}/Libraries
LIBS = -l${TEST_LIBNAME} -l${LIBNAME} -lpromote-${ARCH} -lgnustep-base -pthread -lm
OPT_FLAGS = -O3 -std=c99
WARN_FLAGS = -Wall

COMPILE = ${CXX} ${DEBUG_FLAGS} ${DEF_FLAGS} ${OPT_FLAGS} ${WARN_FLAGS} ${INCLUDE_PATH}
COMPILE_AND_LINK = ${COMPILE} ${LIBRARY_PATH} ${LIBS} 

all: test

clean: 
	@echo -e "\033[7m[Cleaning...]\033[0m"
	rm -f ${TEST_OBJDIROBJS} ${TEST_LIBRARY} include/* build/test-containers build/test-memory

install: lib
	@echo -e "\033[7m[Installing...]\033[0m"
	cp ${TEST_HEADERS} include/

lib: compile_echo ${TEST_OBJDIROBJS}
	@echo -e "\033[7m[Creating library...]\033[0m"
	ar rcs ${TEST_LIBRARY} ${TEST_OBJDIROBJS}

test: install
	@echo -e "\033[7m[Creating test...]\033[0m"
	${COMPILE} src/memory/PerfTestMemory.m -o build/perf-test-memory ${LIBRARY_PATH} ${LIBS}
	${COMPILE} src/memory/UnitTestMemory.m -o build/unit-test-memory ${LIBRARY_PATH} ${LIBS}

compile_echo:
	@echo -e "\033[7m[Compiling...]\033[0m"
	@echo -e ${TEST_SRCDIRS_ALL_HEADERS}

${TEST_OBJDIR}/%.o: src/%.m ${TEST_HEADERS} ${HEADERSDIR}/*.h ${LIBDIR}/lib${LIBNAME}.a
	${COMPILE} -c src/$*.m -o $@

.PHONY: all clean

